package com.oocl.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oocl.pojo.Customer;
import com.oocl.service.CustomerManager;
import com.oocl.service.factory.CustomerManagerFactory;
import com.oocl.util.SpringContextFactory;

//@RestController
@Controller
@RequestMapping("/")
public class CustomerController {
	
	//将applicationContext.xml配置import进webmvc的配置中，只有一个容器
	@Resource(name="customerManager")
	private CustomerManager manager;
	
	public CustomerController() {
//		manager = CustomerManagerFactory.getInstance();
		//此时两个容器，web启动时不会初始化applicaitonContext.xml,需要手动初始化容器对象，不过也可以在SystemListener中初始化
//		manager = SpringContextFactory.getInstance().getBean(CustomerManager.class);
	}
	
	@RequestMapping(value="addCustomer",method=RequestMethod.POST)
	@ResponseBody
	public Customer addCustomer(@RequestBody Customer customer){
		manager.addCustomer(customer);
		return customer;
	}
	
	@RequestMapping(value="updateCustomer",method=RequestMethod.PUT)
	@ResponseBody
	public Customer updateCustomer(@RequestBody Customer customer){
		manager.updateCustomer(customer);
		List<Customer> cus = manager.findAll();
		return customer;
	}
	
	@RequestMapping(value="deleteCustomer",method=RequestMethod.DELETE)
	@ResponseBody
	public Customer deleteCustomer(String cid){
		Customer customer = manager.deleteCustomerById(cid);
		return customer;
	}
	
	@RequestMapping(value="toUpdate",method=RequestMethod.GET)
	@ResponseBody
	public Customer toUpdate(String cid){
		System.out.println(cid);
		Customer customer = manager.findCustomerById(cid);
		return customer;
	}
	
	@RequestMapping("findAll")
	@ResponseBody
	public List<Customer> findAll(){
		List<Customer> customers = manager.findAll();
		return customers;
	}
}
